﻿package code {
	import flash.display.Shape;

	public class Line extends Shape {
		protected var thickness:Number;
		public var a:Vertex;
		public var b:Vertex;
		protected var color:uint;
		
		public function Line(a:Vertex, b:Vertex, thickness:Number, color:uint) {
			this.a = a;
			this.b = b;
			this.thickness = thickness;
			this.color = color;
			init();
		}
		
		public function init() {
			graphics.clear();
			graphics.lineStyle(thickness, color);
			graphics.moveTo(a.x, a.y);
			graphics.lineTo(b.x, b.y);
		}
		
		public function update(a:Vertex, b:Vertex):void {
			this.a = a;
			this.b = b;
			graphics.clear();
			init();
		}
		
		// gets theta (from a to b) in radians:
		public function getAngleBA():Number {
			return getAngle(a,b)
		}
		
		// gets theta (from b to a) in radians:
		public function getAngleAB():Number {
			return getAngle(a,b)
		}
		
		// gets angle 0 to a in radians:
		public function getAngleA():Number {
			return getAngle(new Vertex(0,0),a);
		}
		
		// gets angle 0 to b in radians:
		public function getAngleB():Number {
			return getAngle(new Vertex(0,0),b);
		}
		
		// get angle between q & p:
		private function getAngle(p:Vertex,q:Vertex):Number {
			var xLength:Number = q.x - p.x;
			var yLength:Number = q.y - p.y;
			return Math.atan2(yLength, xLength);
		}
		
		// distance:
		public function distance():Number {
			var xLength:Number = b.x - a.x;
			var yLength:Number = b.y - a.y;
			return Math.sqrt(xLength*xLength + yLength*yLength);
		}
	}
}